package com.hymjweb.demo.framework.menu.mapper;

import com.hymjweb.demo.framework.menu.bean.MenuBean;
import com.hymjweb.demo.framework.menu.bean.MenuPojo;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.framework.menu
 * @Author: luoge
 * @CreateTime: 2021-02-01 11:02
 * @Description:
 */

@Mapper
@Component
public interface MenuMapper {

    class MenuMapperProvider{

        public String getNoPageList(Map param){
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM("xt_menu t ");
                WHERE("1=1 ");
            }}.toString();
            if(param!=null && param.size()>0){
                String pid = DataTypeUtil.toStringObject(param.get("pid"));
                if(pid!=null){
                    sql += " and t.pid = " + DataTypeUtil.quoteStr(pid);
                }
            }
            sql += "order by t.id asc";
            return sql;
        }


        public String queryMenuPojoById(String id){
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM("xt_menu t ");
                WHERE("1=1  and t.id = #{id}");
            }}.toString();
            return sql;
        }


        public String insert(MenuBean menuBean){
            String sql = new SQL() {{
                INSERT_INTO("xt_menu");
                VALUES("id","#{id}");
                VALUES("pid","#{pid}");
                VALUES("icon_cls","#{iconCls}");
                VALUES("name","#{name}");
                VALUES("url","#{url}");
            }}.toString();
            return sql;
        }

        public String update(MenuBean menuBean){
            String sql= new SQL() {{
                UPDATE("xt_menu t");
                SET("t.pid = #{pid}");
                SET("t.icon_cls = #{iconCls}");
                SET("t.name = #{name}");
                SET("t.url = #{url}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String deleteById(String id){
            String sql = new SQL() {{
                DELETE_FROM("xt_menu t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

    }

    @Results(id = "menuMapperId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "icon_cls", property = "iconCls"),
            @Result(column = "name", property = "name"),
            @Result(column = "url", property = "url")
    })
    @SelectProvider(type = MenuMapperProvider.class, method = "getNoPageList")
    List<MenuBean> getNoPageList(Map param);

    @ResultMap("menuMapperId")
    @SelectProvider(type = MenuMapperProvider.class, method = "queryMenuPojoById")
    MenuBean queryMenuPojoById(String id);

    @InsertProvider(type = MenuMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(MenuBean menuBean);

    @UpdateProvider(type = MenuMapperProvider.class, method = "update")
    int update(MenuBean menuBean);

    @DeleteProvider(type = MenuMapperProvider.class,method = "deleteById")
    int deleteById(String id);
}
